package utils

import (
	"fiberx/internal/global"
	"fiberx/internal/global/common"

	"github.com/gofiber/fiber/v3"
	"go.uber.org/zap"
)

var logger *zap.Logger = global.L

// ZapLogger 是一个使用Zap记录日志的Gin中间件
func SystemLogger(c fiber.Ctx, clientInfo common.ClientInfo, serviceCode int, message string) {

	// 根据状态码记录不同级别的日志
	if clientInfo.StatusCode >= fiber.StatusInternalServerError {
		logger.Error(
			"HTTP ERROR",
			zap.Int("status", clientInfo.StatusCode),
			zap.String("method", clientInfo.Method),
			zap.String("path", clientInfo.Path),
			zap.String("ip", clientInfo.IP),
			zap.String("latency", clientInfo.Latency),
			zap.Int("code", serviceCode),
			zap.String("message", message),
		)
	} else if clientInfo.StatusCode >= fiber.StatusBadRequest {
		logger.Warn(
			"HTTP WARNING",
			zap.Int("status", clientInfo.StatusCode),
			zap.String("method", clientInfo.Method),
			zap.String("path", clientInfo.Path),
			zap.String("ip", clientInfo.IP),
			zap.String("latency", clientInfo.Latency),
			zap.Int("code", serviceCode),
			zap.String("message", message),
		)
	} else {
		logger.Info(
			"HTTP SUCCESS",
			zap.Int("status", clientInfo.StatusCode),
			zap.String("method", clientInfo.Method),
			zap.String("path", clientInfo.Path),
			zap.String("ip", clientInfo.IP),
			zap.String("latency", clientInfo.Latency),
			zap.Int("code", serviceCode),
			zap.String("message", message),
		)
	}

}
